package com.health.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.health.dao.UserDAO;
import com.health.exceptions.ValidatorException;
import com.health.exceptions.ValidatorLevelException;
import com.health.model.User;
import com.health.service.IUserService;
import com.health.util.PKGenerator;
import com.health.validator.UserValidator;

public class UserService implements IUserService
{
	private static final Log	log	= LogFactory.getLog(UserService.class);

	private static PKGenerator	generator;

	// Spring inject it
	private UserValidator		validator;

	// Spring inject it
	private UserDAO				dao;

	@SuppressWarnings("static-access")
	public void insert(User u) throws ValidatorLevelException,
			ValidatorException
	{
		String id = generator.userIdGenerator(u);
		u.setId(id);
		String v = validator.validate(u);
		if (null == v || v.equals(""))
		{
			dao.insert(u);
		}
		else
		{
			log.error(v);
			throw new ValidatorException(v);
		}
	}

	public void update(User u)
	{
		dao.update(u);
	}

	public void delete(User u)
	{
		dao.delete(u);
	}

	/**
	 * return a User Object or a String Object
	 * */
	public Object login(User u)
	{
		String email = u.getEmail();
		String password = u.getPassword();
		Object daoRet = dao.login(email, password);
		return daoRet;
	}

	public User selectByPK(User u)
	{
		return dao.selectByPK(u);
	}

	public User selectByPK(String id)
	{
		return dao.selectByPK(id);
	}

	/**
	 * return all users in DB TABLE _USER without START & LIMIT params
	 */
	public List<User> selectListAll(User u)
	{
		return dao.selectListAll(u);
	}

	/**
	 * select a result list include all of the user properties in querycondition
	 * with the START & LIMIT params
	 */
	public List<User> selectListPage(User user)
	{
		return dao.selectListPage(user);
	}

	/**
	 * getters and setters
	 */
	public UserDAO getDao()
	{
		return dao;
	}

	public void setDao(UserDAO dao)
	{
		this.dao = dao;
	}

	public UserValidator getValidator()
	{
		return validator;
	}

	public void setValidator(UserValidator validator)
	{
		this.validator = validator;
	}
}
